<?php
class admin{
	function getUserList( $num  = '' ){
		global $dbs, $user, $page,$encrypt ;
		
		$userInfo = $user -> getUser('');
		$adminPowder = $userInfo['powder'];
		
		if( $adminPowder == 99 )
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'user` WHERE `powder` <= 9 ORDER BY `id` DESC';
		else if($adminPowder == 9  ){
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'user` WHERE `powder` < 9 ORDER BY `id` DESC';
		}
		
		if(  $num  != '' )
			$sql = $sqlCount .' limit 0,'.$num ;
		else
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;;

		
		$listCount	= $dbs -> select($sqlCount);
		$total		= count($listCount);
		
		$lists = $dbs -> select($sql);
		$html .='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
			  .'<tr class="listTitle">'
				//.'<th class="name">头像</th>'
				.'<th class="name">用户</th>'
				.'<th class="name">级别</th>'
				
				//.'<th class="time">注册时间</th>'		
				//.'<th class="score">上传作品</th>'
				.'<th class="hits">帐户状态</th>'	
				//.'<th class="comm">发言次数</th>'							
				.'<th class="operate">'.lang_operate.'</th>'
			  .'</tr>';
		foreach($lists as $list){
			
			$powder = '';
			$powderClass = '';
			$powderClass[$list['powder']] = ' action ';
			$powder = '<a href="'.WEB_ROOT_PATH.'?option=adminDo&type=action&do=changePowder&powder=1&user_id='.$list['id'].'" class="'.$powderClass[1].' normal changePowder"><span>变为 &gt;</span>一般用户</a>'
						.'<a href="'.WEB_ROOT_PATH.'?option=adminDo&type=action&do=changePowder&powder=5&user_id='.$list['id'].'" class="'.$powderClass[5].' advanced changePowder"><span>变为 &gt;</span>高级用户</a>';
			
			if( $adminPowder == 99 ){
				$powder .= '<a href="'.WEB_ROOT_PATH.'?option=adminDo&type=action&do=changePowder&powder=9&user_id='.$list['id'].'" class="'.$powderClass[9].' super changePowder"><span>变为 &gt;</span>管理员</a>' ;			
			}
			 
			
			//	file counts	
			$sql = 'SELECT count(*) as `counts` FROM `'.DB_PRE.'file` WHERE `user_id` = '.$list['id'] ;
			$count = $dbs -> select($sql);
			$fileCount = $count[0]['counts'];
			
			//	comments counts	
			$sql = 'SELECT count(*) as `counts` FROM `'.DB_PRE.'comments` WHERE `user_id` = '.$list['id'] ;
			$count = $dbs -> select($sql);
			$commentCount = $count[0]['counts'];
			
			
			$userStatusClass = '';
			$userStatusClass[$list['status']] = ' action ' ; 			
			$userStatus = '<a href="'.WEB_ROOT_PATH.'option=adminDo&type=action&do=changeStatus&status=1&user_id='.$list['id'].'" class="'.$userStatusClass[1].' valid changeStatus"><span>变为 &gt;</span>有效</a>'
						 .'<a href="./?option=adminDo&type=action&do=changeStatus&status=0&&user_id='.$list['id'].'" class="'.$userStatusClass[0].' locking changeStatus"><span>变为 &gt;</span>锁定</a>';
				
			
			$html .='<tr user_id="'. $list['id'].'">'
					.'<td><div class="itemthumb"><a href="'.urlRewrite('option=home&id='.$list['id']).'" target="_blank"><img src="'.  self::getFace($list['faceSmall']) .'" /></a></div><div class="itemdetail"><h3 class="itemtitle">'.htmlShow($list['nickName']).'</h3><p>'.htmlShow($list['userName']).'</p><p>注册时间：'.date('Y-m-d',strtotime($list['time'])).'</p><p><span>作品：'. $fileCount .'</span><span>发言：'. $commentCount .'</span></p></div></td>'
					
					.'<td class="tags power">'.$powder.'</td>'
				
					//.'<td class="created">'.date('Y-m-d',strtotime($list['time'])).'</td>'
					
					//.'<td class="fileCount">'. $fileCount .'</td>'
					
					.'<td class="tags status">'. $userStatus .'</td>'
					
					//.'<td class="commentCount">'. $commentCount .'</td>' 
					
					.'<td><span class="action del"><a class="del" href="'.WEB_ROOT_PATH.'?option=adminDo&type=action&do=delUser&user_id='.$list['id'].'">删除</a></span></td></tr>'
					
					.'<tr><td colspan="10"><hr /></td></tr>';
						
		}
		$html .= '</table>';
		$html .= getPages($page,$total,getPageFileUrl() ,PAGE_SIZE, PAGE_LEN);
		
		return $html;
		
	}
	
	
	
	function getMediaList(){
		global $dbs, $user, $page, $encrypt ;
		$sqlCount	= 'SELECT * FROM `'. DB_PRE .'file` ORDER BY `id` DESC';
		$sql 		= $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE_BIG ) .' , '. PAGE_SIZE_BIG;
		$listCount	= $dbs -> select($sqlCount);
		$total		= count($listCount);		
		$lists		= $dbs -> select($sql);
		$html 		= '<ul>';		
		foreach($lists as $list){
			$id = $encrypt -> code($list['id']);
			$starHtml =  '';			
			$starHtml = '';
			for($i = 1; $i <= 5 ; $i++){
				$class = $i <= $list['star'] ? ' class="a" ' : '';
				$starHtml .= '<i'.$class.'></i>';
			}	
			$userInfo = $user -> getUser($list['user_id']);
			$html .= '<li>'
			  .'<a href="'.urlRewrite('option=media&id='. $id ).'" target="_blank"><img src="'.getMediaThumb($list['id']).'" /></a>'
			  .'<h2>'. htmlShow($list['name']).'</h2>'
			  .'<h3 class="star">得分：'.$starHtml.'</h3>'
			  .'<h3>上传：<a href="'.urlRewrite('option=home&id='.$list['user_id']) .'  " target="_blank">'. htmlShow($userInfo['nickName']) .'</a></h3>'
			  .'<h3><a href="./?option=userDo&type=action&do=delFile&id='.$id.'" class="del">删除此作品</a></h3>'
			  .'<h1>'. getTag($list['id'],'_blank') .'</h1>'
			.'</li>';
		}
		$html .= '<div class="clear"></div>
		
		</ul>';
		 
		$html .= getPages($page,$total,getPageFileUrl('id=99999',true) ,PAGE_SIZE_BIG, PAGE_LEN);
		return $html;
		
	}
	
	function delUser( $user_id ){
		global $dbs,$user , $dataType ;
		$user_ids = explode(',',$user_id);
		$adminInfo = $user -> getUser();
		// 只能是权力大的删除权利小的。同级之间不能删除
		foreach($user_ids as $id){
			$dbs -> delete( DB_PRE .'user' , ' `id` = '. $user_id .' and `powder` <  '.$adminInfo['powder']);
		}
		exit('{"status":"ok","msg":"已经删除."}');
	}
	
	function changeStatus($user_id,$status){
		global $dbs,$user , $dataType ;
		$status_array = array(0,1); // 不信任客户端
		$userInfo = $user -> getUser('');
		$status = (int)$status ;
		if( !in_array( $status , $status_array )) 
			$status = 1;
		$array['status'] = $status ;
		$array['modified'] = date('Y-m-d H:i:s',time());
		// 只能是权力大的修改权利小的。同级之间不能修改。
		$dbs -> update( DB_PRE .'user' , $array , ' `id` = '. $user_id .' and `powder` < '. $userInfo['powder'] );
		
		exit('{"status":"ok","msg":"成功改变."}');
	}
	
	function changePowder($user_id,$powder){
		global $dbs,$user , $dataType ;
		$powder_array = array(0,1,5,9); // 不信任客户端   // 看客 ， 1: 一般用户，  5: 高级用户   9：管理员
		$userInfo = $user -> getUser('');
		$powder = (int)$powder ;
		if( !in_array( $powder , $powder_array )) 
			$powder = 1;
		$array['powder'] = $powder ;
		$array['modified'] = date('Y-m-d H:i:s',time());
		// 只能是权力大的修改权利小的。同级之间不能修改。
		$dbs -> update( DB_PRE .'user' , $array , ' `id` = '. $user_id .' and `powder` < '. $userInfo['powder'] );
		
		exit('{"status":"ok","msg":"成功改变."}');
	}
	
	
	function getFace($face){
		if( '' == $face )
			$face = USERFACE ;
		return $face;
	}
	
	 

}
?>